package com.suyuan.controller.admin;


import com.github.pagehelper.PageInfo;
import com.suyuan.entity.CompanyInfo;
import com.suyuan.entity.EnvInfor;
import com.suyuan.entity.PigOut;
import com.suyuan.entity.SensorType;
import com.suyuan.service.EnvironmentService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.vo.EnvInforVO;
import com.suyuan.web.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 环境信息类
 */
@Controller
@RequestMapping("/admin/Environment")
public class EnvironmentController {


    @Resource(name = "environmentService")
    private EnvironmentService environmentService;

    private Map<String,Object> msg;

    /**
     * 多条件查询
     * @param beginTime
     * @param endTime
     * @param
     * @return
     */
    @RequestMapping("findByCondition")
    public String findEnvironment(Date beginTime, Date endTime, Page page, Integer aid, ModelMap modelMap){
        PageInfo pageInfo = environmentService.findEnvironment(beginTime,endTime,aid,page);
        modelMap.addAttribute("pageInfo",pageInfo);
        modelMap.addAttribute("aid",aid);
        modelMap.addAttribute("page",page);
        return "environment_list";
    }


    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("toAddInfo")
    public String toAddPigInPage(ModelMap modelMap){
        List<CompanyInfo> companyInfoList =  environmentService.findAllCompany();
        List<SensorType>  sensorTypeList = environmentService.findAllSesenor();
        modelMap.addAttribute("companyInfoList",companyInfoList);
        modelMap.addAttribute("sensorTypeList",sensorTypeList);
        return "environment_add";
    }


    /**
     * 添加环境信息
     * @param envInfor
     * @return
     */
    @RequestMapping("addInfo")
    @ResponseBody
    public Object addEnvInfo(EnvInfor envInfor){
        int code = 0;
        try{
            environmentService.addInfo(envInfor);
            code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }



    /**
     * 批量删除信息
     * @param ids
     * @return
     */
    @RequestMapping("deletes")
    @ResponseBody
    public Object deleteEnvironInfo(String ids) {
        Integer code = environmentService.deleteEnvironInfo(ids);
        if (code == 1) {
            return JsonMessage.success("删除成功");
        } else {
            return JsonMessage.error("删除失败");
        }

    }


    /**
     * 单个删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Integer deleteInfo(Integer id){
        return environmentService.deleteInfo(id);
    }

    /**
     * 跳转编辑页面
     * @param gid
     * @return
     */
    @RequestMapping("toedit")
    public String toedit(Integer gid ,ModelMap modelMap){
        EnvInforVO envInforVO = environmentService.findByGid(gid);
        List<CompanyInfo> companyInfoList = environmentService.findAllCompany();
        List<SensorType> sensorTypes = environmentService.findAllSesenor();
        modelMap.addAttribute("companyInfoList",companyInfoList);
        modelMap.addAttribute("sensorTypes",sensorTypes);
        modelMap.addAttribute("envInforVO",envInforVO);
        return "environment_edit";
    }


    @RequestMapping("edit")
    @ResponseBody
    public Integer updateInfo(EnvInfor envInfor){
        int code = 0;
        try{
            environmentService.updateInfo(envInfor);
            code = 1;
        }catch (Exception e){
            code = 0;
        }
        return code;
    }


}
